package ishaportalunittests.helpers;

import static org.junit.Assert.assertEquals;
import gwtappcontainer.server.apis.APIException;
import gwtappcontainer.server.apis.admin.Roles.Role;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.appengine.tools.development.testing.LocalServiceTestHelper;
import com.google.appengine.tools.development.testing.LocalUserServiceTestConfig;

public class MockGatekeeperTest {
	
	private MockGateKeeper mockGateKeeper;
	
	private final LocalServiceTestHelper userServiceHelper =
		        new LocalServiceTestHelper(new LocalUserServiceTestConfig())
		            .setEnvIsAdmin(true).setEnvIsLoggedIn(true)
		            .setEnvAuthDomain("example.com")
		            .setEnvEmail("test@example.com");
	
	@Before
	public void before() {
		userServiceHelper.setUp();
		
		mockGateKeeper = new MockGateKeeper();
		mockGateKeeper.allowRoleToUser("test@example.com", Role.DEVELOPER);
		mockGateKeeper.allowRoleToUser("test@example.com", Role.PORTAL_READONLY);		
	}
	
	@After
	public void after() {
		userServiceHelper.tearDown();
	}

	@Test
	public void ensureRoleSucceedsForAllowedRole() {	
		User user = UserServiceFactory.getUserService().getCurrentUser();
		
		assertEquals(true, mockGateKeeper.ensureRole(user, Role.DEVELOPER));			
		assertEquals(true, mockGateKeeper.ensureRole(user, Role.PORTAL_READONLY));
	}
	
	@Test(expected = APIException.class)
	public void throwsExceptionForNotAnAllowedRole() {				
		User user = UserServiceFactory.getUserService().getCurrentUser();
		
		assertEquals(true, mockGateKeeper.ensureRole(user, Role.PORTAL_USER));		
	}		
}
